<?php

class user extends xpfw {

    protected $email, $password;

    public function isAuthenticated() {
        $fieldsRequired = array(
            'iduser',
            'firstname',
            'lastname',
            'email',
            'active'
        );
        
        foreach ($fieldsRequired as $field) {
            if (!array_key_exists($field, $_SESSION))
                return false;
        }

        return true;
    }

    public function getData($field) {
        return isset($_SESSION[$field]) ? $_SESSION[$field] : null;
    }
    
    public function setData($field, $value) {
        $_SESSION[$field] = $value;
    }

    public function logout() {
        session_destroy();
    }

    public function login() {
        if ($this->getEmail() && $this->getPassword()) {
            $mysql = new database();

            $result = $mysql->select()
                            ->from('user')
                            ->andWhere(array(
                                'email' => $this->getEmail(),
                                'password' => md5($this->getPassword()),
                                'active' => 1
                            ))->execute();

            if (mysql_num_rows($result) == 1) {
                $userData = mysql_fetch_assoc($result);
                unset($userData['password']);
                $_SESSION = $userData;
                return true;
            }
        }
        return false;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

}

?>
